import processing.core.PApplet;
import processing.core.PConstants;

public class Issue {
	private PApplet parent = null;
	private Tooltip tooltip = null;
	private Priority pri = null;
	
	private int id;
	private int project_issue_id;
	private int status;
	private int resolution;
	private int category;
	private int priority;
	private User submitted_by;
	private User assigned_to;
	private int open_date;
	private int close_date;
//	private String details;
	
	// Drawing Details
	private int X1 = 0;
	private int Y1 = 0;
	private int X2 = 0;
	private int Y2 = 0;
	private int TOP = 0;
	private int BOTTOM = 0;
	private int BGSTROKE = 0;
	private int BGFILL = 0;
		
	public Issue(PApplet p, Tooltip t, Priority prior) {
		this.parent = p;
		this.tooltip = t;
		this.pri = prior;
	}

	public void loadColor() {
		BGSTROKE = parent.color(0,0,0);
		BGFILL = pri.getColor(this.priority);
	}
	
	public void update(int x1, int y1, int x2, int y2, int top, int bottom) {
		X1 = x1;
		Y1 = y1;
		X2 = x2;
		Y2 = y2;
		TOP = top;
		BOTTOM = bottom;
	}
	
	public void display(double curTime, int timeInc) {
		if(Y1 == 0 || Y2 == 0)
			return;

		int y1 = Y1;
		int y2 = Y2;
		
		if(this.open_date > curTime)
			y1 = (int)(Y1-(BOTTOM-TOP)*(this.open_date-curTime)/(timeInc*4));
		if(this.close_date != 0 && this.close_date < curTime)
			y1 = (int)(Y1-(BOTTOM-TOP)*(this.close_date-curTime)/(timeInc*4));

		y2 = y1 + (Y2-Y1);
		
		if(y1 < TOP)
			return;
		
		if(y1 > BOTTOM)
			update(0,0,0,0,0,0);
		
		int alpha = (int)(255*(y2 > Y2 ? (double)Y2/y2 : (double)y2/Y2));
		if(alpha > 255)
			alpha = 255;
		
		loadColor();
		
		parent.stroke(BGSTROKE, alpha);
		parent.fill(BGFILL, alpha);
		parent.strokeWeight(1);

		parent.rectMode(PConstants.CORNERS);
		parent.rect(X1, y1, X2, y2);
		if(!tooltip.isDetails() && X1 <= parent.mouseX && parent.mouseX <= X2 && y1 <= parent.mouseY && parent.mouseY <= y2)
			tooltip.setValue(this);
	}
	
	public boolean isClicked(double curTime, int timeInc) {
		if(Y1 == 0 || Y2 == 0)
			return false;
		
		int y1 = Y1;
		int y2 = Y2;
		
		if(this.open_date > curTime)
			y1 = (int)(Y1-(BOTTOM-TOP)*(this.open_date-curTime)/(timeInc*4));
		if(this.close_date != 0 && this.close_date < curTime)
			y1 = (int)(Y1-(BOTTOM-TOP)*(this.close_date-curTime)/(timeInc*4));

		y2 = y1 + (Y2-Y1);

		if(y1 < TOP || y1 > BOTTOM)
			return false;

		if(X1 <= parent.mouseX && parent.mouseX <= X2 && y1 <= parent.mouseY && parent.mouseY <= y2)
			return true;
		
		return false;
	}
	
	public void setId(int id) {
		this.id = id;
	}

	public int getId() {
		return id;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public int getStatus() {
		return status;
	}

	public void setResolution(int resolution) {
		this.resolution = resolution;
	}

	public int getResolution() {
		return resolution;
	}

	public void setCategory(int category) {
		this.category = category;
	}

	public int getCategory() {
		return category;
	}

	public void setPriority(int priority) {
		this.priority = priority;
		loadColor();
	}

	public int getPriority() {
		return priority;
	}

	public void setSubmittedBy(User submitted_by) {
		this.submitted_by = submitted_by;
	}

	public User getSubmittedBy() {
		return submitted_by;
	}

	public void setAssignedTo(User assigned_to) {
		this.assigned_to = assigned_to;
	}

	public User getAssignedTo() {
		return assigned_to;
	}

	public void setOpenDate(int open_date) {
		this.open_date = open_date;
	}

	public int getOpenDate() {
		return open_date;
	}

	public void setCloseDate(int close_date) {
		this.close_date = close_date;
	}

	public int getCloseDate() {
		return close_date;
	}

	public String toString() {
		String s = "";
		s += "\nIssue ID: "+this.id;
		s += "\n  Priority: "+this.priority;
		s += "\n  Open Date: "+this.open_date;
		s += "\n  Close Date: "+this.close_date;
		s += "\n  Category ID: "+this.category;
		s += "\n  Status ID: "+this.status;
		s += "\n  Resolution ID: "+this.resolution;
		s += "\n  Submitted By: "+this.submitted_by;
		s += "\n  Assigned To: "+this.assigned_to;
		return s;
	}

	public void setProjectIssueId(int project_issue_id) {
		this.project_issue_id = project_issue_id;
	}

	public int getProjectIssueId() {
		return project_issue_id;
	}
}
